package com.cloud.school.bus.teacherhelper.protocol.baseresponse;

import java.io.File;
import java.io.IOException;

import org.apache.http.Header;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpResponseException;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Environment;

import com.android.support.jhf.debug.DebugLog;
import com.android.support.jhf.network.ErrorExceptionHandler;
import com.android.support.jhf.network.IStateListener;
import com.android.support.jhf.network.loopj.android.http.ResponseHandlerInterface;
import com.android.support.jhf.network.loopj.android.http.TextHttpResponseHandler;
import com.android.support.jhf.utils.FileUtils;

/**
 * 返回Json格式的基类
 * 
 * @author lenovo
 * 
 */
abstract public class BaseJsonHttpResponse extends TextHttpResponseHandler {

	protected String code = "";
	
	public BaseJsonHttpResponse() {
	}

	public BaseJsonHttpResponse(String encoding) {
		super(encoding);
	}

	@Override
	public void onSuccess(int statusCode, Header[] headers,
			String responseString, HttpResponse response) {
		DebugLog.logI(responseString);
		
		for (int i = 0; i < headers.length; i++) {
			Header header = headers[i];
			if("code".equalsIgnoreCase(header.getName())){
				code = header.getValue();
				break;
			}
		}
		DebugLog.logI("code : " + code);
		
//		JSONObject jsonObject = null;
//		if (null == (jsonObject = checkJson(responseString))) {
//			sendFailureMessage(statusCode, headers, new byte[0],
//					new HttpResponseException(statusCode,
//							ErrorExceptionHandler.ERR_SERVER_RETURN));
//		}
//		if (!checkResponse(jsonObject)) {
//			sendFailureMessage(
//					statusCode,
//					headers,
//					new byte[0],
//					new HttpResponseException(statusCode, jsonObject.optString(
//							"resultdes", "")));
//		} 
//		else {
			try {
				onSuccess(statusCode, headers, responseString);
				onResponse(this);
			} catch (Throwable e) {
				e.printStackTrace();
				sendFailureMessage(statusCode, headers, new byte[0],
						new HttpResponseException(statusCode,
								ErrorExceptionHandler.ERR_SERVER_RETURN));
			}

//		}
	}

	/**
	 * 判断是否是json格式
	 * 
	 * @param responseString
	 * @return null不是json格式
	 */
	private JSONObject checkJson(String responseString) {
		JSONObject jsonObject = null;
		try {
			jsonObject = new JSONObject(responseString);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return jsonObject;
	}

	/**
	 * 判断返回数据是否正确，
	 * 
	 * @param jsonObject
	 * @return true正确，false服务器提示错误信息
	 */
	private boolean checkResponse(JSONObject jsonObject) {
		String result = jsonObject.optString("result", null);
		if (null == result || result.equalsIgnoreCase("false")) {
			return false;
		}
		if (result.equalsIgnoreCase("sucess")) {
			return true;
		}
		return true;
	}

	abstract public void onSuccess(int statusCode, Header[] headers,
			String responseString) throws Throwable;

	public void onResponse(ResponseHandlerInterface responseHandlerInterface) {
	}

}
